package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysFaultPms;
import com.ruoyi.system.domain.SysStatisticsDetails;
import com.ruoyi.system.service.ISysFaultPmsService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * PMS缺陷信息Controller
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@CrossOrigin
@Controller
@RequestMapping("/system/pms")
public class SysFaultPmsController extends BaseController {
    private String prefix = "system/pms";

    @Autowired
    private ISysFaultPmsService sysFaultPmsService;

//    @RequiresPermissions("system:pms:view")
    @GetMapping()
    public String pms() {
        return prefix + "/pms";
    }

    /**
     * 查询PMS缺陷信息列表
     */
//    @RequiresPermissions("system:pms:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysFaultPms sysFaultPms) {
        startPage();
        List<SysFaultPms> list = sysFaultPmsService.selectSysFaultPmsList(sysFaultPms);
        return getDataTable(list);
    }

    /**
     * 导出PMS缺陷信息列表
     */
//    @RequiresPermissions("system:pms:export")
    @Log(title = "PMS缺陷信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysFaultPms sysFaultPms) {
        List<SysFaultPms> list = sysFaultPmsService.selectSysFaultPmsList(sysFaultPms);
        ExcelUtil<SysFaultPms> util = new ExcelUtil<SysFaultPms>(SysFaultPms.class);
        return util.exportExcel(list, "PMS缺陷信息数据");
    }

    /**
     * 新增PMS缺陷信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存PMS缺陷信息
     */
//    @RequiresPermissions("system:pms:add")
    @Log(title = "PMS缺陷信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysFaultPms sysFaultPms) {
        return toAjax(sysFaultPmsService.insertSysFaultPms(sysFaultPms));
    }

    /**
     * 修改PMS缺陷信息
     */
//    @RequiresPermissions("system:pms:edit")
    @GetMapping("/edit/{faultId}")
    public String edit(@PathVariable("faultId") Long faultId, ModelMap mmap) {
        SysFaultPms sysFaultPms = sysFaultPmsService.selectSysFaultPmsByFaultId(faultId);
        mmap.put("sysFaultPms", sysFaultPms);
        return prefix + "/edit";
    }

    /**
     * 修改保存PMS缺陷信息
     */
//    @RequiresPermissions("system:pms:edit")
    @Log(title = "PMS缺陷信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysFaultPms sysFaultPms) {
        return toAjax(sysFaultPmsService.updateSysFaultPms(sysFaultPms));
    }

    /**
     * 删除PMS缺陷信息
     */
//    @RequiresPermissions("system:pms:remove")
    @Log(title = "PMS缺陷信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysFaultPmsService.deleteSysFaultPmsByFaultIds(ids));
    }

    /**
     * 导入缺陷信息
     */
    @ApiOperation("导入pms缺陷信息")
//    @RequiresPermissions("system:pms:import")
    @Log(title = "缺陷信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(@RequestPart MultipartFile file, @RequestParam(defaultValue = "false") boolean updateSupport) throws Exception {
        ExcelUtil<SysFaultPms> util = new ExcelUtil<SysFaultPms>(SysFaultPms.class);
        List<SysFaultPms> list = util.importExcel(file.getInputStream());
        String message = sysFaultPmsService.importSysFaultPms(list, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 导出pms缺陷信息模板
     */
    @ApiOperation("导出pms缺陷信息模板")
//    @RequiresPermissions("system:pms:view")
    @Log(title = "缺陷信息", businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<SysFaultPms> util = new ExcelUtil<SysFaultPms>(SysFaultPms.class);
        return util.importTemplateExcel("缺陷信息导入模板");
    }

    /**
     * 查询缺陷信息列表--by--time
     */
    @ApiOperation("查询缺陷信息列表--by--time")
//    @RequiresPermissions("system:pms:list")
    @PostMapping("/faultPmsTotal")
    @ResponseBody
    public AjaxResult faultTotal(Date time1, Date time2, SysFaultTotalParameters totalParameters) {
        //时间对象转字符串对象  并完成字符串拼接
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String t1 = formatter.format(time1);
        String t2 = formatter.format(time2);
        String data_2 = t1 + " " + t2;
        // 数据查询参数构建
        HashMap params = totalParameters.getParams();
        List<SysFaultPms> list = sysFaultPmsService.selectSysFaultListByTime(time1, time2);
        //筛选 参数读取

        SysStatisticsDetails statisticsDetails = new SysStatisticsDetails(data_2, list, time1, time2, params);
        statisticsDetails.setInitParams(totalParameters);
        return AjaxResult.success(statisticsDetails);
    }
}

@ApiModel(value = "TotalParameters", description = "缺陷筛选排除参数")
class SysFaultTotalParameters {

    @ApiModelProperty("缺陷来源 [PMS,管控]")
    private String sourceTotal;

//    @ApiModelProperty("电压等级 [交流35kV,交流220kV]")
//    private String voltageLevelTotal;

    @ApiModelProperty("班组 [班组1,班组2]")
    private String groupTotal;

//    @ApiModelProperty("缺陷类型 [类型1,类型2]")
//    private String faultTyTotal;

    @ApiModelProperty("缺陷性质 [一般,严重,危急]")
    private String degreeTotal;

    @ApiModelProperty("消缺状态 [已消缺,未消缺]")
    private String isSolveTotal;

    @ApiModelProperty("发生时段 [周期内缺陷,周期外缺陷]")
    private String occurrenceTotal;

    public SysFaultTotalParameters(String sourceTotal, String groupTotal, String degreeTotal, String isSolveTotal, String occurrenceTotal) {
        this.sourceTotal = sourceTotal;
        this.groupTotal = groupTotal;
        this.degreeTotal = degreeTotal;
        this.isSolveTotal = isSolveTotal;
        this.occurrenceTotal = occurrenceTotal;
    }

    public HashMap getParams() {
        HashMap params = new HashMap<>();
        if (this.sourceTotal != null) {
            if(this.sourceTotal != ""){
                String[] ls = this.sourceTotal.split(",");
                for (String l : ls) {
                    params.put(l, l);
                }
            }
        }
//        if (this.voltageLevelTotal != null) {
//            if(this.voltageLevelTotal != ""){
//                String[] ls = this.voltageLevelTotal.split(",");
//                for (String l : ls) {
//                    params.put(l, l);
//                }
//            }
//
//        }
        if (this.groupTotal != null) {
            if(this.groupTotal != ""){
                String[] ls = this.groupTotal.split(",");
                for (String l : ls) {
                    params.put(l, l);
                }
            }

        }
//        if (this.faultTyTotal != null) {
//            if(this.faultTyTotal != ""){
//                String[] ls = this.faultTyTotal.split(",");
//                for (String l : ls) {
//                    params.put(l, l);
//                }
//            }
//
//        }
        if (this.degreeTotal != null) {
            if(this.degreeTotal != ""){
                String[] ls = this.degreeTotal.split(",");
                for (String l : ls) {
                    params.put(l, l);
                }
            }

        }
        if (this.isSolveTotal != null) {
            if(this.isSolveTotal != ""){
                String[] ls = this.isSolveTotal.split(",");
                for (String l : ls) {
                    params.put(l, l);
                }
            }

        }
        if (this.occurrenceTotal != null) {
            if(this.occurrenceTotal != ""){
                String[] ls = this.occurrenceTotal.split(",");
                for (String l : ls) {
                    params.put(l, l);
                }
            }

        }
        return params;
    }

    public String getSourceTotal() {
        return sourceTotal;
    }

    public void setSourceTotal(String sourceTotal) {
        this.sourceTotal = sourceTotal;
    }

//    public String getVoltageLevelTotal() {
//        return voltageLevelTotal;
//    }
//
//    public void setVoltageLevelTotal(String voltageLevelTotal) {
//        this.voltageLevelTotal = voltageLevelTotal;
//    }

    public String getGroupTotal() {
        return groupTotal;
    }

    public void setGroupTotal(String groupTotal) {
        this.groupTotal = groupTotal;
    }

//    public String getFaultTyTotal() {
//        return faultTyTotal;
//    }
//
//    public void setFaultTyTotal(String faultTyTotal) {
//        this.faultTyTotal = faultTyTotal;
//    }

    public String getDegreeTotal() {
        return degreeTotal;
    }

    public void setDegreeTotal(String degreeTotal) {
        this.degreeTotal = degreeTotal;
    }

    public String getIsSolveTotal() {
        return isSolveTotal;
    }

    public void setIsSolveTotal(String isSolveTotal) {
        this.isSolveTotal = isSolveTotal;
    }

    public String getOccurrenceTotal() {
        return occurrenceTotal;
    }

    public void setOccurrenceTotal(String occurrenceTotal) {
        this.occurrenceTotal = occurrenceTotal;
    }
}

